﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;

namespace MyWeb.Admins
{
	public partial class Config : System.Web.UI.Page
	{
		static string Lang = "";
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!IsPostBack)
			{
				Lang = Session["Lang"].ToString();
                List<Data.Config> listE = ConfigService.Config_GetByAll(Lang);
                if(listE.Count > 0)
                {
                    Session["Insert"] = "false";
                    hfid.Value = "1";
                    txtFacebook.Text = listE[0].Facebook;
                    txtWellcome.Text = listE[0].Wellcome;
                    txtPlaceHead.Text = listE[0].PlaceHead;
                    txtPlaceBody.Text = listE[0].PlaceBody;
                    txtMail_Smtp.Text = listE[0].Mail_Smtp;
                    txtMail_Port.Text = listE[0].Mail_Port;
                    txtMail_Info.Text = listE[0].Mail_Info;
                    txtMail_Noreply.Text = listE[0].Mail_Noreply;
                    txtMail_Password.Text = listE[0].Mail_Password;
                    txtGooglega.Text = listE[0].Googlega;
                    txtGoogleId.Text = listE[0].GoogleId;
                    ckContact.Text = listE[0].Contact;
                    ckCopyright.Text= listE[0].Copyright;
                    txtTitle.Text = listE[0].Title;
                    txtDescription.Text = listE[0].Description;
                    txtKeyword.Text = listE[0].Keyword;
                    LoadStyleDropDownList();
                    ddlStyle.Text = listE[0].StyleId;
                }
                else
                {
                    ControlClass.ResetControlValues(this);
                    Session["Insert"] = "true";
                }
			    pnUpdate.Visible = true;
			}
            ckContact.Language = "vi";
            ckCopyright.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(ckContact);
            _FileBrowser.SetupCKEditor(ckCopyright);
		}

		protected void Update_Click(object sender, EventArgs e)
		{
			if (Page.IsValid){
				Data.Config obj = new Data.Config();
                obj.Id = hfid.Value;
                obj.Facebook = txtFacebook.Text;
                obj.Wellcome = txtWellcome.Text;
				obj.PlaceHead = txtPlaceHead.Text;
                obj.PlaceBody = txtPlaceBody.Text;
                obj.Googlega = txtGooglega.Text;
				obj.GoogleId = txtGoogleId.Text;
                obj.Mail_Smtp = txtMail_Smtp.Text;
                obj.Mail_Port = txtMail_Port.Text;
                obj.Mail_Info = txtMail_Info.Text;
                obj.Mail_Noreply = txtMail_Noreply.Text;
                obj.Mail_Password = txtMail_Password.Text;
                obj.Contact = ckContact.Text;
				obj.Copyright = ckCopyright.Text;
				obj.Title = txtTitle.Text;
				obj.Description = txtDescription.Text;
                obj.Keyword = txtKeyword.Text;
                obj.StyleId = ddlStyle.SelectedValue;
				obj.Lang = Lang;
                if (Session["Insert"].Equals("true"))
                {
					ConfigService.Config_Insert(obj);
				}
				else{
					ConfigService.Config_Update(obj);
				}
                //pnUpdate.Visible = false;
                Session["Insert"] = "false";
			}
		}

		protected void Back_Click(object sender, EventArgs e)
		{
            //pnUpdate.Visible = false;
            Session["Insert"] = "false";
		}
        private void LoadStyleDropDownList()
		{
			ddlStyle.Items.Clear();
			ddlStyle.Items.Add(new ListItem("Chọn giao diện", ""));
			List<Data.Style> list = StyleService.Style_GetByAll();
			for (int i = 0; i < list.Count; i++)
			{
				ddlStyle.Items.Add(new ListItem(list[i].Name, list[i].Id));
			}
			ddlStyle.DataBind();
		}
	}
}